package com.selebash.runtime;

import com.selebash.ast.AbstractAst;
import com.selebash.exception.SelebashException;
import java.util.List;

/**
 * @author sad
 */
public class NestedExecutionProcessor {

    /**
     * if returns true, next rule in current sequence will be executed. In outer case this sequence will be treated as passed
     */
    public boolean afterStep(Exception ex,ExecutionContext executionContext) {
        if (ex != null) {
            throw new RuntimeException(ex);
        }
        return true;
    }

    public boolean afterExecution(ExecutionContext executionContext) {
        return true;
    }

    public void run(ExecutionContext executionContext, List<AbstractAst>nodes) {
        processInnerNodes(executionContext, nodes);
        afterExecution(executionContext);
    }
    
    public void processInnerNodes(ExecutionContext executionContext, List<AbstractAst>nodes){
     for (AbstractAst node : nodes) {
            Exception exception = null;
            try {
                node.process(executionContext);
            } catch (SelebashException ex) {
                exception = ex;
            }
            boolean result = afterStep(exception, executionContext);
            if (result == false) {
                break;
            }
        }
    }

}
